
#### 3.1 MODEL INPUT LEGITIMATION TECHNOCRATIC ####

## Packages and data

pacman::p_load(DBI, RSQLite, dplyr, readr, stringr, fixest, tidyr, modelsummary, kableExtra)

con <- dbConnect(RSQLite::SQLite(), "./data/iso_standards.sqlite") 
participation <- dbReadTable(con, "participants")
dbDisconnect(con)

standards2 <- readRDS("./data/standards2.rds")
organzation_type <- read_rds("./data/organizations_tagged.rds")

exp_data <- read_rds("./data/exp_data.rds")

## Making data ##

member_bodies <- participation %>%
  filter(membership %in% c("P-member", "Secretariat")) %>%
  mutate(continent = countrycode::countrycode(sourcevar = country,
                                              origin = "country.name",
                                              destination = "country.name"))  %>%
  mutate(committee = str_replace(committee, "(\\d+) /", "\\1/")) %>%
  mutate(committee = str_remove(committee, "ISO/IEC")) %>%
  mutate(committee = str_remove(committee, "ISO/")) %>%
  filter(!committee %in% c("JPC 2", "REMCO", "ASCO", "TMB/WG SR", "TMBG/CCCC", "TMBG/TF 7")) %>%
  filter(!str_detect(committee, "PC ")) %>%
  filter(year <= 2021 & year >= 2004)  %>%
  select(year, country, continent, committee, sector) %>%
  distinct(year, committee, continent, .keep_all = TRUE)  %>%
  group_by(year, committee) %>%
  add_count() %>%
  ungroup()

tech_leg0 <- member_bodies %>%
  left_join(exp_data, by = join_by(year, country)) %>%
  left_join(standards2, by = join_by(year, committee)) %>%
  distinct(committee, year, country, .keep_all = TRUE) %>%
  drop_na(physical, societal) 

tech_leg <- tech_leg0 %>%
  group_by(committee, year, sector, n, physical, societal) %>%
  summarise(GDEXPRD = mean(rndgdp, na.rm = TRUE),
            RESEARCHER = mean(researchers, na.rm = TRUE),
            hightechexp = mean(hightechexp, na.rm = TRUE),
            unicollab = mean(unicollab, na.rm = TRUE),
            scienceart = mean(scienceart, na.rm = TRUE),
            businessgerd = mean(businessgerd, na.rm = TRUE),
            knowledgeemp = mean(knowledgeemp, na.rm = TRUE)) %>%
  ungroup() %>%
  drop_na(committee, year, physical, societal)

## Models ##

mod1 <- feols(physical ~ GDEXPRD | year + committee,
               cluster = "committee+year",
               data = tech_leg)
mod2 <- feols(physical ~ RESEARCHER | year + committee,
               cluster = "committee+year",
               data = tech_leg)
mod3 <- feols(physical ~ hightechexp | year + committee,
              cluster = "committee+year",
              data = tech_leg)
mod4 <- feols(physical ~ unicollab | year + committee,
              cluster = "committee+year",
              data = tech_leg)
mod5 <- feols(physical ~ scienceart | year + committee,
              cluster = "committee+year",
              data = tech_leg)
mod6 <- feols(physical ~ businessgerd | year + committee,
              cluster = "committee+year",
              data = tech_leg)
mod7 <- feols(physical ~ knowledgeemp | year + committee,
              cluster = "committee+year",
              data = tech_leg)

models <- list(mod1, mod2, mod3, mod4, mod5, mod6, mod7)

cm <- c("GDEXPRD" = "Committee expertise",
       "RESEARCHER" = "Committee expertise",
       "hightechexp" = "Committee expertise",
       "unicollab" = "Committee expertise",
       "scienceart" = "Committee expertise",
       "businessgerd" = "Committee expertise",
       "knowledgeemp" = "Committee expertise")

names(models) <- c("R&D % of GDP", 
                   "Researchers per million people",
                   "High-tech exports, % total trade",
                   "University-industry R&D collaboration",
                   "Scientific and technical articles/bn PPP$ GDP",
                   "GERD performed by business, % GDP",
                   "Knowledge-intensive employment, %")

mod <- modelsummary(models,
                    stars = TRUE,
                    gof_omit = 'AIC|BIC|Within|Std.Errors|RMSE|FE',
                    fmt = 3,
                    coef_map = cm,
                    #output = "latex",
                    statistic = c("{std.error}"),
                    notes = c("Fixed effects: Committee and year",
                              "Clustered standard errors by committee and year",
                              "Coverage: 2004 - 2021")) %>%
  add_header_above(c(" " = 1, "Number of physical standards produced in technical committee" = 7)) %>%
  kable_styling(font_size = 9, full_width = FALSE)

mod

#save_kable(mod, file = "./Tables/iso_mod1_tech.tex")

# Models with societal standards

mod1_s <- feols(societal ~ GDEXPRD | year + committee,
              cluster = "committee+year",
              data = tech_leg)
mod2_s <- feols(societal ~ RESEARCHER | year + committee,
              cluster = "committee+year",
              data = tech_leg)
mod3_s <- feols(societal ~ hightechexp | year + committee,
              cluster = "committee+year",
              data = tech_leg)
mod4_s <- feols(societal ~ unicollab | year + committee,
              cluster = "committee+year",
              data = tech_leg)
mod5_s <- feols(societal ~ scienceart | year + committee,
              cluster = "committee+year",
              data = tech_leg)
mod6_s <- feols(societal ~ businessgerd | year + committee,
              cluster = "committee+year",
              data = tech_leg)
mod7_s <- feols(societal ~ knowledgeemp | year + committee,
              cluster = "committee+year",
              data = tech_leg)

models_s <- list(mod1_s, mod2_s, mod3_s, mod4_s, mod5_s, mod6_s, mod7_s)

cm <- c("GDEXPRD" = "Committee expertise",
        "RESEARCHER" = "Committee expertise",
        "hightechexp" = "Committee expertise",
        "unicollab" = "Committee expertise",
        "scienceart" = "Committee expertise",
        "businessgerd" = "Committee expertise",
        "knowledgeemp" = "Committee expertise")

names(models_s) <- c("R&D % of GDP", 
                   "Researchers per million people",
                   "High-tech exports, % total trade",
                   "University-industry R&D collaboration",
                   "Scientific and technical articles/bn PPP$ GDP",
                   "GERD performed by business, % GDP",
                   "Knowledge-intensive employment, %")

mod <- modelsummary(models_s,
                    stars = TRUE,
                    gof_omit = 'AIC|BIC|Within|Std.Errors|RMSE|FE',
                    fmt = 3,
                    coef_map = cm,
                    #output = "latex",
                    statistic = c("{std.error}"),
                    notes = c("Fixed effects: Committee and year",
                              "Clustered standard errors by committee and year",
                              "Coverage: 2004 - 2021")) %>%
  add_header_above(c(" " = 1, "Number of societal standards produced in technical committee" = 7)) %>%
  kable_styling(font_size = 9, full_width = FALSE)

mod

#save_kable(mod, file = "./Tables/iso_mod1_tech_societal.tex")

## Coefficient plot

# Physical

coef_gdp_physical <- broom::tidy(feols(physical ~ GDEXPRD | year + committee,
                                       cluster = "committee+year",
                                       data = tech_leg %>%
                                         drop_na(GDEXPRD) %>%
                                         mutate(physical = scale(physical, center = TRUE, scale = TRUE),
                                                GDEXPRD = scale(GDEXPRD, center = TRUE, scale = TRUE))),
                             conf.int = TRUE) %>% mutate(term = ifelse(term == "GDEXPRD", "R&D", NA))

coef_researchers_physical <- broom::tidy(feols(physical ~ RESEARCHER | year + committee,
                                               cluster = "committee+year",
                                               data = tech_leg %>%
                                                 drop_na(RESEARCHER) %>%
                                                  mutate(physical = scale(physical, center = TRUE, scale = TRUE),
                                                         RESEARCHER = scale(RESEARCHER, center = TRUE, scale = TRUE))),
                                          conf.int = TRUE) %>% mutate(term = ifelse(term == "RESEARCHER", "Researchers", NA))


coef_hightech_physical <- broom::tidy(feols(physical ~ hightechexp | year + committee,
                                               cluster = "committee+year",
                                               data = tech_leg %>%
                                                 drop_na(hightechexp) %>%
                                                 mutate(physical = scale(physical, center = TRUE, scale = TRUE),
                                                        hightechexp = scale(hightechexp, center = TRUE, scale = TRUE))),
                                         conf.int = TRUE) %>% mutate(term = ifelse(term == "hightechexp", "High-tech export", NA))

coef_unicollab_physical <- broom::tidy(feols(physical ~ unicollab | year + committee,
                                               cluster = "committee+year",
                                               data = tech_leg %>%
                                                 drop_na(unicollab) %>%
                                                 mutate(physical = scale(physical, center = TRUE, scale = TRUE),
                                                        unicollab = scale(RESEARCHER, center = TRUE, scale = TRUE))),
                                         conf.int = TRUE) %>% mutate(term = ifelse(term == "unicollab", "Uni-Industry collab.", NA))

coef_scienceart_physical <- broom::tidy(feols(physical ~ scienceart | year + committee,
                                               cluster = "committee+year",
                                               data = tech_leg %>%
                                                 drop_na(RESEARCHER) %>%
                                                 mutate(physical = scale(physical, center = TRUE, scale = TRUE),
                                                        scienceart = scale(scienceart, center = TRUE, scale = TRUE))),
                                         conf.int = TRUE) %>% mutate(term = ifelse(term == "scienceart", "Articles", NA))

coef_businessgerd_physical <- broom::tidy(feols(physical ~ businessgerd | year + committee,
                                               cluster = "committee+year",
                                               data = tech_leg %>%
                                                 drop_na(businessgerd) %>%
                                                 mutate(physical = scale(physical, center = TRUE, scale = TRUE),
                                                        businessgerd = scale(businessgerd, center = TRUE, scale = TRUE))),
                                         conf.int = TRUE) %>% mutate(term = ifelse(term == "businessgerd", "Business GERD", NA))

coef_knowledgeemp_physical <- broom::tidy(feols(physical ~ knowledgeemp | year + committee,
                                               cluster = "committee+year",
                                               data = tech_leg %>%
                                                 drop_na(knowledgeemp) %>%
                                                 mutate(physical = scale(physical, center = TRUE, scale = TRUE),
                                                        knowledgeemp = scale(knowledgeemp, center = TRUE, scale = TRUE))),
                                         conf.int = TRUE) %>% mutate(term = ifelse(term == "knowledgeemp", "Knowledge emp.", NA))

# Societal

coef_gdp_societal <- broom::tidy(feols(societal ~ GDEXPRD | year + committee,
                                       cluster = "committee+year",
                                       data = tech_leg %>%
                                         drop_na(GDEXPRD) %>%
                                         mutate(societal = scale(societal, center = TRUE, scale = TRUE),
                                                GDEXPRD = scale(GDEXPRD, center = TRUE, scale = TRUE))),
                                 conf.int = TRUE) %>% mutate(term = ifelse(term == "GDEXPRD", "R&D", NA))

coef_researchers_societal <- broom::tidy(feols(societal ~ RESEARCHER | year + committee,
                                               cluster = "committee+year",
                                               data = tech_leg %>%
                                                 drop_na(RESEARCHER) %>%
                                                 mutate(societal = scale(societal, center = TRUE, scale = TRUE),
                                                        RESEARCHER = scale(RESEARCHER, center = TRUE, scale = TRUE))),
                                         conf.int = TRUE) %>% mutate(term = ifelse(term == "RESEARCHER", "Researchers", NA))

coef_hightech_societal <- broom::tidy(feols(societal ~ hightechexp | year + committee,
                                            cluster = "committee+year",
                                            data = tech_leg %>%
                                              drop_na(hightechexp) %>%
                                              mutate(societal = scale(societal, center = TRUE, scale = TRUE),
                                                     hightechexp = scale(hightechexp, center = TRUE, scale = TRUE))),
                                      conf.int = TRUE) %>% mutate(term = ifelse(term == "hightechexp", "High-tech export", NA))

coef_unicollab_societal <- broom::tidy(feols(societal ~ unicollab | year + committee,
                                             cluster = "committee+year",
                                             data = tech_leg %>%
                                               drop_na(unicollab) %>%
                                               mutate(societal = scale(societal, center = TRUE, scale = TRUE),
                                                      unicollab = scale(RESEARCHER, center = TRUE, scale = TRUE))),
                                       conf.int = TRUE) %>% mutate(term = ifelse(term == "unicollab", "Uni-Industry collab.", NA))

coef_scienceart_societal <- broom::tidy(feols(societal ~ scienceart | year + committee,
                                              cluster = "committee+year",
                                              data = tech_leg %>%
                                                drop_na(RESEARCHER) %>%
                                                mutate(societal = scale(societal, center = TRUE, scale = TRUE),
                                                       scienceart = scale(scienceart, center = TRUE, scale = TRUE))),
                                        conf.int = TRUE) %>% mutate(term = ifelse(term == "scienceart", "Articles", NA))

coef_businessgerd_societal <- broom::tidy(feols(societal ~ businessgerd | year + committee,
                                                cluster = "committee+year",
                                                data = tech_leg %>%
                                                  drop_na(businessgerd) %>%
                                                  mutate(societal = scale(societal, center = TRUE, scale = TRUE),
                                                         businessgerd = scale(businessgerd, center = TRUE, scale = TRUE))),
                                          conf.int = TRUE) %>% mutate(term = ifelse(term == "businessgerd", "Business GERD", NA))

coef_knowledgeemp_societal <- broom::tidy(feols(societal ~ knowledgeemp | year + committee,
                                                cluster = "committee+year",
                                                data = tech_leg %>%
                                                  drop_na(knowledgeemp) %>%
                                                  mutate(societal = scale(societal, center = TRUE, scale = TRUE),
                                                         knowledgeemp = scale(knowledgeemp, center = TRUE, scale = TRUE))),
                                          conf.int = TRUE) %>% mutate(term = ifelse(term == "knowledgeemp", "Knowledge emp.", NA))


coef_plot_input_tech <- bind_rows(
  coef_gdp_physical %>% mutate(dv = "R&D", model = "Technocratic input legitimation", standard = "physical"),
  coef_researchers_physical %>% mutate(dv = "Researchers", model = "Technocratic input legitimation", standard = "physical"),
  coef_hightech_physical %>% mutate(dv = "High-tech export", model = "Technocratic input legitimation", standard = "physical"),
  coef_unicollab_physical %>% mutate(dv = "Uni-Industry collab.", model = "Technocratic input legitimation", standard = "physical"),
  coef_scienceart_physical %>% mutate(dv = "Articles", model = "Technocratic input legitimation", standard = "physical"),
  coef_businessgerd_physical %>% mutate(dv = "Business GERD", model = "Technocratic input legitimation", standard = "physical"),
  coef_knowledgeemp_physical %>% mutate(dv = "Knowledge emp.", model = "Technocratic input legitimation", standard = "physical")) %>%
  bind_rows(coef_gdp_societal %>% mutate(dv = "R&D", model = "Technocratic input legitimation", standard = "societal"),
            coef_researchers_societal %>% mutate(dv = "Researchers", model = "Technocratic input legitimation", standard = "societal"),
            coef_hightech_societal %>% mutate(dv = "High-tech export", model = "Technocratic input legitimation", standard = "societal"),
            coef_unicollab_societal %>% mutate(dv = "Uni-Industry collab.", model = "Technocratic input legitimation", standard = "societal"),
            coef_scienceart_societal %>% mutate(dv = "Articles", model = "Technocratic input legitimation", standard = "societal"),
            coef_businessgerd_societal %>% mutate(dv = "Business GERD", model = "Technocratic input legitimation", standard = "societal"),
            coef_knowledgeemp_societal %>% mutate(dv = "Knowledge emp.", model = "Technocratic input legitimation", standard = "societal")) %>%
  na.omit()


## Clean 

rm(list=ls()[! ls() %in% c("coef_plot_input_dem", "coef_plot_input_tech", "coef_plot_output_dem", "coef_plot_output_tech",
                           "liaison_dat_merged_sh", "liaison_dat_merged_frac", "participation_dat_merged_sh", "participation_dat_merged_frac",
                           "tech_leg",
                           "output_tech_leg", "output_dem_leg",
                           "legitimacy_coding")])
gc()
